﻿/*
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.UnitTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Dynamic;
    using Moq;
    /// <summary>
    /// Common methods to create mocked objects
    /// </summary>
    static class MockObjects
    {

        public static IAmoServer GetMockedDisconnectedServer()
        {
            var mock = new Mock<IAmoServer>();
            mock.SetupGet(x => x.Connected).Returns(false);
            IAmoServer server = mock.Object;
            return server;
        }

        public static IAmoServer GetMockedConnectedServer()
        {
            var mock = new Mock<IAmoServer>();
            mock.SetupGet(x => x.Connected).Returns(true);
            mock.SetupGet(x => x.ServerMode).Returns("Multidimensional");
            IAmoServer server = mock.Object;
            return server;
        }

        public static IPartitionManager GetMockedPartitionManager()
        {
            IAmoServer server = GetMockedConnectedServer();
            return new PartitionManager(server, GetMockedListOfPartitions());
        }

        public static dynamic GetMockedDynamic()
        {
            dynamic newDynamic = new ExpandoObject();
            newDynamic.SsasDatabaseName = "Database1";
            newDynamic.SsasModelType = "Multidimensional";
            newDynamic.SsasCubeName = "Cube1";
            newDynamic.SsasMeasureGroupName = "MeasureGroup1";
            newDynamic.PartitionName = "Partition1";
            newDynamic.PartitionWhereClause = "Fun != UnitTesting";
            newDynamic.PartitionSlice = "[Some].[Fancy].&[MDX]";
            return newDynamic;
        }

        public static IPartition GetMockedPartition()
        {
            return Partition.FromDynamic(GetMockedConnectedServer(), GetMockedDynamic());
        }

        public static IEnumerable<IPartition> GetMockedListOfPartitions()
        {
            List<IPartition> partitions = new List<IPartition>();
            partitions.Add(GetMockedPartition());
            return partitions;
        }

        public static IPartitionRepository GetMockedRepository()
        {
            var mockPartitionRep = new Mock<IPartitionRepository>();

            return (IPartitionRepository)mockPartitionRep.Object;
        }

        public static IPartitionRepository GetMockedPopulatedRepository(IAmoServer server)
        {
            var mockPartitionRep = new Mock<IPartitionRepository>();

            mockPartitionRep
                .Setup(x => x.GetListOfPartitions(server))
                .Returns(GetMockedListOfPartitions());

            mockPartitionRep
                .Setup(x => x.SsasServerName("DEV"))
                .Returns("localhost");

            return (IPartitionRepository)mockPartitionRep.Object;
        }
    }
}
